package com.dudy.service.oa.leave;

import com.dudy.dao.LeaveDAO;
import com.dudy.entity.Leave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Random;

/**
 * @author <a href="mailto:wb-zhaojunwei@alibaba-inc.com">wb-zhaojunwei></a>
 * @version 1.0
 * @Description 类说明
 * @since 2016/3/10
 */
@Component
@Transactional
public class LeaveManager {
    private LeaveDAO leaveDao;

    public Leave getLeave(Long id) {
        return leaveDao.findOne(id);
    }

    @Transactional(readOnly = false)
    public void saveLeave(Leave entity) {
        if (entity.getId() == null) {
            entity.setApplyTime(new Date());
        }
        try {
            leaveDao.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            entity.setId(new Random().nextLong());
        }
    }

    @Autowired
    public void setLeaveDao(LeaveDAO leaveDao) {
        this.leaveDao = leaveDao;
    }
}
